/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Usuario;

import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
 *
 * @author nan
 */
import java.awt.event.KeyEvent;
import javax.swing.InputMap;
import javax.swing.KeyStroke;



public class MedUsuarios implements ActionListener, KeyListener{

    private Usuarios ventanaPrin = new Usuarios();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();

    public MedUsuarios(){
        InputMap map = new InputMap();

        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "pressed");
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), "released");
        ventanaPrin.getjButtonAceptar().setInputMap(0, map);
        ventanaPrin.getjButtonAceptar().setInputMap(0, map);
        //------------------------------------
        ventanaPrin.setActionListeners(this);
        ventanaPrin.setKeyListener(this);
        ventanaPrin.setLocationRelativeTo(null);
        ventanaPrin.show();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == ventanaPrin.getjButtonAceptar()){
            conectar.conectar();
            Statement st;
            try {
                String nomUser = ventanaPrin.getjTextFieldUsuario().getText();
                String contra = ventanaPrin.getjPasswordField1().getText();
                st = conectar.getConn().createStatement();
                String consulta = "SELECT * FROM \"Personas\" WHERE \"NomUsuario\" ='"+nomUser+"' AND \"Contrasenia\" = '"+contra+"';" ;
                ResultSet rs = st.executeQuery(consulta);
                String Usuario = null;
                String id = null;
                String tipouser = null;
                while (rs.next()){
                    Usuario = rs.getString("NomUsuario");
                    id = rs.getString("Contrasenia");
                    tipouser = rs.getString("TipoUser");
                }
                if (tipouser == null){
                    JOptionPane.showMessageDialog(null, "Ingrese usuario y contraseña", "Error por usuario o contraseña", JOptionPane.ERROR_MESSAGE);
                }else{
                    if (tipouser.matches("Admin")|tipouser.matches("Usuario")){
                        ventanaPrin.dispose();
                        new Administracion.AdministracionMediador(Usuario,tipouser);
                    }else{
                        JOptionPane.showMessageDialog(null, "Usuario sin permiso para el sistema", "Error por usuario sin permiso", JOptionPane.ERROR_MESSAGE);
                    }
                }
            } catch (Exception ex){
                ex.printStackTrace();
            }
            conectar.cerrar();
        }else{
            if(e.getSource() == ventanaPrin.getjButtonCancelar()){
                ventanaPrin.dispose();
            }
        }
    }

    public void keyTyped(KeyEvent e) {   
        if (e.getSource() == ventanaPrin.getjTextFieldUsuario() && e.getKeyChar() == KeyEvent.VK_ENTER){
            ventanaPrin.getjPasswordField1().requestFocus();
        } else if (e.getSource() == ventanaPrin.getjPasswordField1() && e.getKeyChar() == KeyEvent.VK_ENTER){
            ventanaPrin.getjButtonAceptar().doClick();
        }
        

    }

    public void keyPressed(KeyEvent e) {
         
    }
    public void keyReleased(KeyEvent e) {    }

}
